package com.hue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hue.entity.Goods;
import com.hue.entity.Orders;
import com.hue.entity.User;
import com.hue.entity.vo.GoodsVo;
import com.hue.mapper.OrdersMapper;
import com.hue.service.GoodsService;
import com.hue.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚凤桥
 * @since 2021-07-16
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private GoodsService goodsService;


    @Override
    public Orders createOrder(User user, GoodsVo goods) {
        //生成订单
        Orders orders = new Orders();
        orders.setId((long)(Math.random()*100000000));
        orders.setUserId(Long.parseLong(user.getMobile()));
        orders.setGoodsId(goods.getId());
        orders.setGoodsCount(1);
        orders.setGoodsPrice(goods.getSeckillPrice());
        orders.setStatus(2);
        orders.setCreateTime(new Date());
        orders.setPayTime(new Date());
        save(orders);
        //商品总数量-1
        QueryWrapper<Goods> wrapper =  new QueryWrapper<>();
        wrapper.eq("id",goods.getId());
        Goods one = goodsService.getOne(wrapper);
        one.setGoodsStock(one.getGoodsStock()-1);
        goodsService.updateById(one);
        return orders;
    }
}
